using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.UI.WebControls;

namespace IRL.Utilities
{
    /// <summary>
    /// Summary description for List
    /// </summary>
    public class List
    {
        private static object connStr = ConfigurationSettings.AppSettings["sqlConn"];

        // Method:  fillChk
        // Created: 23-Jul-07
        // Action:  - Fill CheckboxList Control item
        public static void fillChk(CheckBoxList chk, string sql)
        {
            if (connStr != null)
            {
                SqlConnection conn = new SqlConnection(connStr.ToString());
                SqlCommand cmd = new SqlCommand(sql, conn);
                SqlDataReader dr;
                try
                {
                    conn.Open();
                    dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                    chk.DataValueField = dr.GetName(0);
                    if (dr.FieldCount == 1)
                        chk.DataTextField = dr.GetName(0);
                    else
                        chk.DataTextField = dr.GetName(1);

                    chk.DataSource = dr;
                    chk.DataBind();
                }
                catch
                {
                    HttpContext.Current.Response.Redirect("unavailable.htm");
                }
                finally
                {
                    conn.Close();
                }

            }
        }

        // Method:  fillList
        // Action:  - Fill DropDownList item
        public static void fillList(DropDownList lst, string sql)
        {
            try
            {
                if (connStr != null)
                {
                    //ListItem li = new ListItem("", "-1");
                    using (SqlConnection conn = new SqlConnection(connStr.ToString()))
                    {
                        SqlCommand cmd = new SqlCommand(sql, conn);
                        conn.Open();
                        SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                        lst.DataValueField = dr.GetName(0);
                        if (dr.FieldCount == 1)
                            lst.DataTextField = dr.GetName(0);
                        else
                            lst.DataTextField = dr.GetName(1);

                        lst.DataSource = dr;
                        lst.DataBind();
                        //lst.Items.Insert(0,li);                    
                    }
                }
            }
            catch
            {
                HttpContext.Current.Response.Redirect("unavailable.htm");
            }

        }

        // Function set DropDownList value
        public static void setList(DropDownList lst, string value)
        {
            foreach (ListItem li in lst.Items)
            {
                if (li.Value == value)
                {
                    li.Selected = true;
                    return;
                }
            }
        }

        public static void setList(DropDownList lst, string value, string text)
        {
            foreach (ListItem li in lst.Items)
            {
                if (li.Value == value)
                {
                    li.Selected = true;
                    return;
                }
            }
            ListItem li_New = new ListItem(text, value);
            lst.Items.Insert(0, li_New);
        }
    }
}
